{% extends "shared/layout.volt" %}
{% block content %}
<h1 class="page-header">商户信息</h1>
<div class="form-box form-app-box">
    <form class="form-horizontal">
        <input type="hidden" name="app_id" value="">
        <div class="form-title-box">
            <h3 class="view-title">基础信息</h3>
            <a href="{{ url('merchant/edit') }}?mch_id={{ Model.merchant_id }}" class="pull-right">修改</a>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">商户名称</label>
            <div class="col-sm-10">
                <p class="form-control-static">{{ Model.merchant_name }}</p>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">商户状态</label>
            <div class="col-sm-10">
                <p class="form-control-static">
                    {% if Model.merchant_status == 'y' %}
                        <span class="label-yes"><i class="glyphicon glyphicon-ok-circle"></i> 启用</span>
                    {% else %}
                        <span class="label-no"><i class="glyphicon glyphicon-ban-circle"></i> 停用</span>
                    {% endif %}
                </p>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">应用图标</label>
            <div class="col-sm-10">
                <p class="form-control-static"><img src="{{ Model.merchant_image_url }}" onerror="this.src='/assets/images/app.png';" style="width: 87px;height: 87px;"> </p>
            </div>
        </div>
        <div class="form-title-box">
            <h3 class="view-title pull-left">支付选项</h3>
            <a href="{{ url('home/edit') }}?mch_id={{ Model.merchant_id }}" class="btn btn-primary-light btn-sm pull-right">添加支付</a>
            <div class="clearfix"></div>
        </div>
        <div class="form-group" style="padding: 0 15px;">
            <table class="table-tidy" id="merchantPayTable">
                <thead class="table-tidy-head">
                <tr>
                    <th style="width: 220px;">支付名称</th>
                    <th>支付介绍</th>
                    <th style="width: 120px;">支付状态</th>
                    <th style="width: 120px;">操作</th>
                </tr>
                </thead>
                <tbody class="table-tidy-body">
                {% for item in PayList %}
                <tr>
                    <td>{{ item.pay_name }}</td>
                    <td>{{ item.remark }}</td>
                    <td>
                        {% if item.pay_status == 'y' %}
                        <span class="text-label-success">•</span> 启用
                        {% else %}
                            <span class="text-label-fail">•</span> 禁用
                        {% endif %}
                    </td>
                    <td>
                        <a href="javascript:;" class="btn-action-delete" data-pay-id="{{ item.pay_id }}" data-merchant-id="{{ Model.merchant_id }}" title="删除配置">删除</a> |
                        <a href="{{ url('home/detail') }}?pay_id={{ item.pay_id }}" title="查看支付配置"> 查看</a>
                    </td>
                </tr>
                {% else %}
                    <tr>
                        <td colspan="4" class="text-center">暂无数据</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="form-title-box">
            <h3 class="view-title">开发配置</h3>
        </div>
        <div class="form-small-title-box">
            <h3 class="view-title">基础环境</h3>
        </div>
        <div class="form-group" style="padding: 0 15px;">
            <table class="table-tidy">
                <tbody class="table-tidy-body">
                <tr>
                    <td class="first-col">支付网关</td>
                    <td class="second-col">{{ Gateway }}</td>
                </tr>
                <tr>
                    <td class="first-col">应用网关</td>
                    <td class="second-col">
                        <span id="merchantGatewayWrapper">{{ Model.gateway }}</span>
                        <a href="javascript:;" data-target="#merchantGatewayDisplayModal" data-toggle="modal" data-value="{{ Model.gateway }}">修改</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="form-small-title-box">
            <h3 class="view-title">接口加签方式</h3>
        </div>
        <div class="form-group" style="padding: 0 15px;">
            <table class="table-tidy">
                <tbody class="table-tidy-body">
                <tr>
                    <td class="first-col">RSA2(SHA256)密钥</td>
                    <td class="second-col">
                        <a href="javascript:;"  data-target="#merchantDisplayModal" data-toggle="modal">查看商户公钥</a> |
                        <a href="javascript:;" data-value="{{ RsaPublicCert }}" data-target="#systemDisplayModal" data-toggle="modal">查看系统公钥</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </form>
</div>
    <!-- Modal -->
    <div class="modal fade" id="merchantDisplayModal" tabindex="-1" role="dialog" aria-labelledby="merchantDisplayModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="merchantDisplayModalTitle">商户公钥(SHA256withRsa)</h4>
                </div>
                <div class="modal-body">
                    <div id="merchantDisplayModalView">
                        <pre id="merchantDisplayModalBody" style="background-color: white;font-size: 14px;border: none;">{{ Model.merchant_public_cert }}</pre>
                        <a href="javascript:editEncryption();" id="btnEditEncryption">修改密钥</a>
                    </div>
                    <div id="merchantDisplayModalWrapper" style="display: none;">
                        <form method="post" id="merchantDisplayModalForm" action="{{ url('merchant/editEncryption') }}">
                            <input type="hidden" name="merchant_id" value="{{ Model.merchant_id }}">
                            <textarea name="merchant_public_cert" class="form-control textarea" style="height: 320px;resize: none;margin-bottom: 15px;">{{ Model.merchant_public_cert }}</textarea>

                            <button type="submit" class="btn btn-primary-light btn-sm" data-loading-text="保存中...">保存</button>
                        </form>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="merchantGatewayDisplayModal" tabindex="-1" role="dialog" aria-labelledby="gatewayDisplayModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">商户网关</h4>
                </div>
                <div class="modal-body">
                    <div id="merchantGatewayDisplayModalWrapper">
                        <form method="post" id="merchantGatewayDisplayModalForm" action="{{ url('merchant/editGateway') }}" class="form-horizontal">
                            <input type="hidden" name="merchant_id" value="{{ Model.merchant_id }}">
                            <div class="form-group">
                                <label for="app_id" class="col-sm-2 control-label">商户网关</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="gateway" value="{{ Model.gateway }}" id="merchantGateway">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-8">
                                    <button type="submit" class="btn btn-primary-light btn-sm" data-loading-text="保存中...">保存</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="systemDisplayModal" tabindex="-1" role="dialog" aria-labelledby="systemDisplayModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">支付系统公钥</h4>
                </div>
                <div class="modal-body">
                    <pre style="background-color: white;font-size: 14px;border: none;">{{ RsaPublicCert }}</pre>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block footer %}
<script type="text/javascript">
    $(function () {
        $("#merchantDisplayModal").on('show.bs.modal',function (e) {
            $("#merchantDisplayModalView").show();
            $("#merchantDisplayModalWrapper").hide();
        });
        $(".btn-action-delete").on("click",function () {
            var pay_id = $(this).attr("data-pay-id");
            var mch_id = $(this).attr("data-merchant-id");
            var $then = $(this);
            $.ajax({
                url : "{{ url('home/delete') }}",
                type : "post",
                dataType : "json",
                data : {"merchant_id":mch_id,"pay_id": pay_id},
                success : function ($res) {
                    if($res.errCode === 0 || $res.errCode === '0'){
                        $then.parents('tr').remove().empty();
                        if($("#merchantPayTable>tbody>tr").length <= 0){
                            $("#merchantPayTable>tbody").html('<tr><td colspan="4" class="text-center">暂无数据</td></tr>');
                        }

                        layer.msg($res.errMsg,{time : 1500,icon :6});
                    }else{
                        layer.msg($res.errMsg,{time : 1500,icon :2});
                    }
                },
                error : function () {
                    layer.msg("服务器异常",{icon :2,time:1500});
                }
            })
        });
    });
    /**
     * 编辑密钥
     */
    function editEncryption() {
        $("#merchantDisplayModalView").hide();
        $("#merchantDisplayModalWrapper").show();
    }

    /**
     * 保存商户公钥
     */
    $("#merchantDisplayModalForm").ajaxForm({
        type : "POST",
        dataType :"json",
        resetForm: true,
        beforeSubmit : function () {
            $("#merchantDisplayModalForm").find("button[type='submit']").button('loading');
        },
        success : function ($res) {
            if($res.errCode === 0 || $res.errCode === '0'){
                $("#merchantDisplayModal").modal('hide');
                $("#merchantDisplayModalBody").text($res.merchant_public_cert);
                $("#merchantDisplayModalForm").find('textarea').val($res.merchant_public_cert)
                layer.msg('保存成功');
            }else{
                layer.msg('保存失败')
            }
            $("#merchantDisplayModalForm").find("button[type='submit']").button('reset');
        },
        error : function () {
            layer.msg('保存失败');
            $("#merchantDisplayModalForm").hide().find("button[type='submit']").button('reset');
            $("#merchantDisplayModalView").show();
        }
    });
    /**
     * 保存商户网关
     */
    $("#merchantGatewayDisplayModalForm").ajaxForm({
        type : "POST",
        dataType : "json",
        beforeSubmit : function () {
            $("#merchantGatewayDisplayModalForm").find("button[type='submit']").button('loading');
        },
        success : function ($res) {
            if($res.errCode === 0 || $res.errCode === '0'){
                $("#merchantGatewayDisplayModal").modal('hide');
                $("#merchantGateway").val($res.gateway);
                $("#merchantGatewayWrapper").text($res.gateway);
                layer.msg("保存成功");
            }else{
                layer.msg($res.errMsg);
            }
            $("#merchantGatewayDisplayModalForm").find("button[type='submit']").button('reset');
        },
        error : function () {
            $("#merchantGatewayDisplayModalForm").find("button[type='submit']").button('reset');
        }
    });

</script>
{% endblock %}